package com.woniuxy.student.consumer.controller;

import com.woniuxy.entity.ResponseResult;
import com.woniuxy.entity.Student;
import com.woniuxy.student.consumer.feginclient.StudentFeignClient;
import com.woniuxy.student.consumer.service.SomeService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author 作者
 * @create 2025-06-26 14:31
 */
@RefreshScope
@Slf4j
@RequestMapping("/consumerstudent")
@RestController
public class ConsumerStudentControll {

//    @Autowired
//    RestTemplate restTemplate;

//    public String getUrl(){
//        return  "http://localhost:8081/student/";
//    }

//    @Autowired
//    private DiscoveryClient discoveryClient;
//    public String getUrl(){
//        List<ServiceInstance> list = discoveryClient.getInstances("student-provider");
//        if (list != null && list.size() > 0 ) {
//            return list.get(0).getUri()+"/student/";
//        }
//        return null;
//    }

    @Value("${server.port}")
    int port;

//    @Autowired
//    private LoadBalancerClient loadBalancerClient;
//
//    public String getUrl() {
//        ServiceInstance serviceInstance = loadBalancerClient.choose("student-provider");
//        if (serviceInstance != null ) {
//            return serviceInstance.getUri() + "/student/";
//        }
//        return null;
//    }

//    public String getUrl() {
//        //resttemplate+@loadbalancer注解，可以自动去服务注册中心根据服务名或服务地址
//        //如果是一个集群列表，自动根据负载均衡算法，默认使用轮询，选择一个可用的服务
//        return "http://student-provider/student/";
//    }

    //接口的实现类 StudentMapper底层就是AOP
    @Autowired
    StudentFeignClient studentFeignClient;

    //微服务调用应该写在service
    @GetMapping("/")
    public ResponseResult<List<Student>> getAll() {
        log.info("7071调用8081查询学生信息");
        return studentFeignClient.getAll();
    }

    @GetMapping("/{sid}")
    public ResponseResult<Student> getBySid(@PathVariable("sid") int sid) {
        log.info("7071调用8081查询学生信息");
        return studentFeignClient.getBySid(sid);
//        try {
//            log.info("7071调用8081查询学生信息");
//            return studentFeignClient.getBySid(sid);
//        } catch (Exception e) {
//            return ResponseResult.fail(null);
//        }
    }

    @PostMapping("/")
    public ResponseResult<Void> add(@RequestBody Student student) {
        return studentFeignClient.add(student);
//        return ResponseResult.ok();
    }


    @Value("${my.test}")
    private String mytest;

    @GetMapping("/mytest")
    public ResponseResult mytest() {
        System.out.println(mytest);
        return ResponseResult.ok(mytest);
    }

    @Autowired
    SomeService someService;
    @GetMapping("/get")
    public ResponseResult get() {
        //查询商品
        someService.doSome();
        return ResponseResult.ok("get");
    }
    @GetMapping("/add")
    public ResponseResult add() {
        //添加订单
        someService.doSome();
        return ResponseResult.ok("add");
    }

    @GetMapping("/test")
    public ResponseResult test(@RequestHeader(value = "token",defaultValue = "xxx")String token ,
                               @RequestHeader(value = "name",defaultValue = "yyy")String name ,
                               HttpServletRequest request) {
        log.info("token:{},name:{}",token,name);
        log.info("token:{},name:{}",request.getHeader("token"),request.getHeader("name"));
        return ResponseResult.ok("test");
    }



}
